package com.cwh.taskcenter.task;

import cn.hutool.core.lang.UUID;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.cwh.taskcenter.mq.MessageProducer;
import com.cwh.taskcenter.task.api.TaskServiceI;
import com.cwh.taskcenter.task.dto.data.TaskPageVO;
import com.cwh.taskcenter.task.dto.event.UserBehaviorEvent;
import com.cwh.taskcenter.task.dto.query.TaskListByPageQry;
import com.cwh.taskcenter.task.executor.command.ReceiveTaskCmdExe;
import com.cwh.taskcenter.task.executor.command.TaskAddCmdExe;
import com.cwh.taskcenter.task.executor.command.TaskEventHandleExe;
import com.cwh.taskcenter.task.executor.query.TaskByPageQryExe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author cwh
 */
@Service
public class TaskServiceImpl implements TaskServiceI {

    @Autowired
    private TaskAddCmdExe taskAddCmdExe;

    @Autowired
    private ReceiveTaskCmdExe receiveTaskCmdExe;

    @Autowired
    private TaskEventHandleExe taskEventHandleExe;

    @Autowired
    private TaskByPageQryExe taskByPageQryExe;

    @Autowired
    private MessageProducer messageProducer;

    @Value("${kafka.topics.user-behavior}")
    private String taskEventTopic;


    @Override
    public Response addTask(TaskAddCmd taskAddCmd) {
        return taskAddCmdExe.execute(taskAddCmd);
    }

    @Override
    public Response receive(TaskReceiveCmd cmd) {
        return receiveTaskCmdExe.execute(cmd);
    }

    @Override
    public Response eventReport(TaskEventReportCmd cmd) {
        UUID uuid = UUID.fastUUID();
        cmd.getUserBehaviorEvent().setUuid(uuid.toString());
        messageProducer.send(taskEventTopic, null, cmd.getUserBehaviorEvent());
        return Response.buildSuccess();

    }

    @Override
    @Transactional
    public Response handleEvent(UserBehaviorEvent event) {
        return taskEventHandleExe.execute(event);
    }

    @Override
    public PageResponse<TaskPageVO> pageTask(TaskListByPageQry qry) {
        return taskByPageQryExe.execute(qry);
    }

}
